/************************************************************************************
* Collector.h                                                                       *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/
#ifndef COLLECTOR_H
#define COLLECTOR_H

#include <QAbstractItemModel>
#include <QHash>
#include <QList>
#include <QStandardItemModel>


#include "CollectorItem.h"
#include "Types.h"
#include "widgets/StarWidget.h"
#include "CollectionDeleterThread.h"



class Collector : public QAbstractItemModel
{
    Q_OBJECT

    static Collector *__instance;

    QList<CollectorItem*>           m_items;
    QHash<QString, CollectorItem*>* m_itemMap;
    QHash<QString, ArtistInfo *>    m_artistInfo;
    CollectionDeleterThread        *m_thread;
    bool                            m_valid;

    explicit Collector(QObject *parent = 0);
private slots:
    void populateAuxStrcts();
    void clearAuxStructs();
    void addToAuxStructs(CollectorItem *track);
    void addToAuxStructs(QList<CollectorItem *>* tracks);
    void removeFromAuxStructs(QList<QString>* tackIds);


signals:
    void aboutToRemoveItems(QList<QString>* trackId);
    void itemsAdded(QList<CollectorItem *>* items);
    void modelItemChanged(MediaItem* item);
    void collectionDeletionStarted();
    void collectionDeletionEnded();

public slots:
    void add(QList<CollectorItem*>* items);
    void remove(QList<QString>* ids);
    void itemPlayed(MediaItem* item);
    void itemFinishedPlaying(MediaItem* item);
    void clear();
    void threadCompletedRemoval();
    void mediaRatingChanged(CollectorItem *item, MediaRating rating);

public:
    ~Collector();
    quint32 numItems();
    CollectorItem *item(QString uuid);
    int items(QList<QString> &trackIds, QHash<QString, CollectorItem *> &outItems);
    int items(QList<QString> &trackIds, QList<CollectorItem *> &outItems);
    int itemsFromArtist(QString artistName, QList<CollectorItem *> &outItems);
    int itemsFromAlbum(QString albumName, QString artistName, QList<CollectorItem *> &outItems);
    int albumsFromArtist(QString artistName, QList<AlbumInfo *> &albums);
    QList<ArtistInfo *> allArtists();
    ArtistInfo *artist(QString artistName);
    CollectorItem *item(int row);

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    QModelIndex index(int row, int column, const QModelIndex &parent) const;
    QModelIndex parent(const QModelIndex &child) const;
    bool hasChildren(const QModelIndex &parent) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    void setValid(bool value);

    bool isValid();

    static void init(QWidget *parent);
    static Collector *get();
};

#endif // COLLECTORMODEL_H
